package com.example.zhongxiangmo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;

/**
 * Created by 文龙 on 2019/12/30.
 */

public class XiangMuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private ArrayList<BannerBean.DataBean> databanners;
    private ArrayList<ArticleBean.DataBean.DatasBean> list;
    private int ITEM_TYPE_ONE = 1;
    private int ITEM_TYPE_TWO = 2;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public XiangMuAdapter(Context context, ArrayList<BannerBean.DataBean> databanners, ArrayList<ArticleBean.DataBean.DatasBean> list) {
        this.context = context;
        this.databanners = databanners;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == ITEM_TYPE_ONE) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_banner, null);

            ViewHolderOne viewHolderOne = new ViewHolderOne(inflate);
            return viewHolderOne;

        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_article, null);

            ViewHolderTwo viewHolderTwo = new ViewHolderTwo(inflate);

            return viewHolderTwo;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == ITEM_TYPE_ONE) {
            ViewHolderOne viewHolderOne = (ViewHolderOne) holder;
            ArrayList<String> title = new ArrayList<>();
            ArrayList<String> image = new ArrayList<>();

            for (int i = 0; i < databanners.size(); i++) {
                String url = databanners.get(i).getImagePath();
                String title1 = databanners.get(i).getTitle();
                title.add(title1);
                image.add(url);
            }
            viewHolderOne.mBanner
                    .setImages(image)
                    .setBannerTitles(title)
                    .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE)
                    .setImageLoader(new GlideImageLoader())
                    .start();
        } else {
            ViewHolderTwo viewHolderTwo = (ViewHolderTwo) holder;
            viewHolderTwo.mTvTitle.setText(list.get(position - 1).getTitle());
            viewHolderTwo.mTvRight.setText(list.get(position-1).getNiceDate());
            viewHolderTwo.mTvShijian.setText(list.get(position-1).getNiceShareDate());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(position);
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                onItemLongClickListener.OnItemLongClick(position);

                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_TYPE_ONE;
        } else {
            return ITEM_TYPE_TWO;
        }
    }

    static class ViewHolderOne extends RecyclerView.ViewHolder {
        Banner mBanner;

        ViewHolderOne(View view) {
            super(view);
            this.mBanner = (Banner) view.findViewById(R.id.banner);
        }
    }


    class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).into(imageView);
        }
    }

    static class ViewHolderTwo extends RecyclerView.ViewHolder {
        TextView mTvRight;
        TextView mTvTitle;
        TextView mTvShijian;

        ViewHolderTwo(View view) {
            super(view);
            this.mTvRight = (TextView) view.findViewById(R.id.tv_right);
            this.mTvTitle = (TextView) view.findViewById(R.id.tv_title);
            this.mTvShijian = (TextView) view.findViewById(R.id.tv_shijian);
        }
    }

    public interface OnItemClickListener{
        void OnItemClick(int i);
    }
    public interface OnItemLongClickListener{
        void OnItemLongClick(int i);
    }
}
